<template>
	<view class="myCoupon">
		<view class="tabs">
			<block v-for="item in tabs" :key="item.val">
				<view class="type_item" @tap="clickType(item.val)">
					<view class="type_name" :class="{'active_name':item.val==couponData.type}">{{item.name}}</view>
					<view class="under_line" :class="{'active_line':item.val==couponData.type}"></view>
				</view>
			</block>
		</view>
		<view class="list">
			<block v-for="(item,index) in list" :key="index">
				<view class="coupon_item">
					<view class="item_t">
						<view class="">
							<view class="coupon_title">{{ item.name }}</view>
							<view class="use_time">{{ item.useStartTimeStr }}-{{ item.useEndTimeStr }}</view>
						</view>
						<view class="t-c">
							<view class="price" :class="{'can_use':item.status===0}">
								<text>￥</text>
								<text class="price_val">{{ item.money }}</text>
							</view>
							<view class="use_limits" :class="{'can_use':item.status===0}" v-if="parseInt(item.minPrice)">满{{ item.minPrice }}可用</view>
							<view class="use_limits" :class="{'can_use':item.status===0}" v-else>无门槛</view>
						</view>
					</view>
					<view class="item_b">
						<view class="use_desc">
							<view class="desc_title">
								<text>本券仅限商城内小程序使用</text>
								<image src="/static/mine/xia.png" mode="" :class="{'open_arrow':item.isOpen}" @tap="openDesc(item,index)"></image>
							</view>
							<view class="desc_text" v-if="item.isOpen">{{ item.useIntroduce }}</view>
						</view>
						<view class="coupon_status" :class="{'can_use':item.status===0}">
							{{item.status===0?'待使用':(item.status===1?'已使用':'已失效')}}
						</view>
					</view>
				</view>
			</block>
			<view style="text-align: center;margin-top: 100rpx;" v-if="list.length==0">
				<image style="width: 200rpx;height: 200rpx;" src="/static/nodata.png"></image>
				<view style="color: #999;text-align: center;">暂无数据</view>
			</view>
			<view style="text-align: center;color: #999;font-size: 24rpx;" v-if="noMoreData&&list.length!=0">
				没有更多啦
			</view>
		</view>
		
		<view class="coupon_center" @click="$to(`/pages/index/useCoupon/useCoupon`)">
			<image src="../static/coupons.png" mode="widthFix" />
			<view class="">领券中心</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [
					{
						name: '全部',
						val: 'all'
					}, {
						name: '未使用',
						val: 'usable'
					}, {
						name: '已使用',
						val: 'used'
					}, {
						name: '已过期',
						val: 'expired'
					},
				],
				currentType: 'all',
				list: [],
				couponData: {
					type: 'all',
					limit: 10,
					page: 1,
					search: '',
				},
				totalPage: 1,
				noMoreData: false,
			};
		},
		onShow() {
			this.getCouponList();
		},
		onReachBottom() {
			if (this.couponData.page < this.totalPage) {
				this.couponData.page += 1
				this.getCouponList()
			} else {
				this.noMoreData = true
			}
		},
		methods: {
			clickType(type) {
				this.couponData.type = type;
				this.couponData.page = 1;
				this.getCouponList();
			},
			openDesc(item, index) {
				this.$set(this.list[index], 'isOpen', !item.isOpen)
				console.log(this.list)
			},
			async getCouponList() {
				const res = await this.$api.get('coupon/list', this.couponData);
				if (res.code == 200) {
					var arr = [],totalPage = 1;
					if( res.data !== 'null' && res.data !== null) {
						totalPage = res.data.totalPage;
						arr = res.data.list || [];
						arr.forEach(v => {
							v.isOpen = false;
						})
					}
					if (this.couponData.page == 1) {
						this.list = arr || [];
						this.totalPage = totalPage || 1;
					} else {
						this.list.push(...arr);
					}
					this.page != 1 && this.list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}

	.myCoupon {
		.tabs {
			width: 100%;
			padding: 36rpx 44rpx 14rpx;
			box-sizing: border-box;
			background-color: #ffffff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: fixed;
			left: 0;
			top: 0;
			right: 0;
			z-index: 6;

			.type_item {
				display: flex;
				flex-direction: column;
				align-items: center;

				.type_name {
					font-size: 26rpx;
					line-height: 26rpx;
					color: #999999;
					font-weight: 400rpx;
					margin-bottom: 10rpx;
				}

				.active_name {
					color: #4F98F6;
					font-weight: bold;
				}

				.under_line {
					width: 54rpx;
					height: 6rpx;
					border-radius: 3rpx;
				}

				.active_line {
					background-color: #4F98F6;
				}
			}
		}

		.list {
			padding: 120rpx 26rpx 30rpx;
			box-sizing: border-box;

			.coupon_item {
				width: 100%;
				border-radius: 20rpx;
				background-color: #fff;
				padding: 36rpx 36rpx 20rpx;
				box-sizing: border-box;
				margin-bottom: 14rpx;

				.item_t {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 46rpx;

					.coupon_title {
						font-size: 30rpx;
						line-height: 30rpx;
						font-weight: bold;
						margin-bottom: 20rpx;
					}

					.use_time {
						font-size: 22rpx;
						line-height: 22rpx;
						color: #999999;
					}

					.price {
						font-size: 26rpx;
						font-weight: bold;
						color: #B3B3B3;
						margin-bottom: 10rpx;

						.price_val {
							font-size: 60rpx;
							line-height: 32rpx;
						}
					}

					.use_limits {
						font-size: 24rpx;
						line-height: 24rpx;
					}
					.can_use{
						color: #FF4747;
					}
				}

				.item_b {
					display: flex;
					justify-content: space-between;

					.use_desc {
						.desc_title {
							display: flex;
							align-items: center;
							font-size: 22rpx;
							color: #999999;
							margin-bottom: 14rpx;

							image {
								width: 20rpx;
								height: 11rpx;
								margin-left: 40rpx;
							}

							.open_arrow {
								transform: rotate(180deg);
							}
						}

						.desc_text {
							width: 424rpx;
							font-size: 22rpx;
							color: #999999;
							line-height: 26rpx;
						}
					}

					.coupon_status {
						width: 130rpx;
						height: 46rpx;
						text-align: center;
						line-height: 46rpx;
						background-color: #E6E6E6;
						border-radius: 10rpx;
						color: #B2B2B2;
						font-size: 24rpx;
					}

					.can_use {
						background-color: #2E80FE;
						color: #fff;
					}
				}
			}
		}
	
		.coupon_center {
			font-size: 24rpx;
			text-align: center;
			position: fixed;
			right: 40rpx;
			bottom: 120rpx;
			image {
				width: 80rpx;
				height: 80rpx;
			}
		}
	}
</style>